﻿CREATE PROCEDURE [dbo].[usp_ResetBatchForResubmission]
	@BatchID					uniqueidentifier,
	@Debug						bit			   = 0
AS

	IF @Debug = 1
	BEGIN
		print 'usp_ResetBatchForResubmission: @vCurrentTime = ' + CONVERT(varchar(50), GETDATE())
	END

	-- Mark the Batch and the Batch Messages for resubmission
	BEGIN TRY

		BEGIN TRANSACTION T1

			UPDATE BatchResubmission
				SET 
				StartDatetime = NULL, 
				EndDatetime =  NULL, 
				[Status] = N'Pending'
			WHERE
				BatchID = @BatchID

			UPDATE BatchResubmissionMessage
				SET [Status] = N'Pending', 
				ErrorMessage = NULL, 
				ProcessingID = NULL, 
				ProcessingServer = NULL
			WHERE
				MessageBatchID = @BatchID
				AND [Status] NOT IN (N'Completed', N'Unsupported')

		COMMIT TRANSACTION T1
	END TRY
	BEGIN CATCH
		DECLARE @error int, @message varchar(4000), @xstate int;
        SELECT @error = ERROR_NUMBER(), @message = ERROR_MESSAGE(), @xstate = XACT_STATE();

		ROLLBACK TRANSACTION T1
        
		RAISERROR ('[usp_ResetBatchForResubmission]: %d: %s', 16, 1, @error, @message) ;
        RETURN;
	END CATCH

RETURN 0
